package com.controller;

import com.pojo.Carouse;
import com.service.CarousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/carouse")
public class CarouseServiceController {
    @Autowired
    private CarousService carousService;
    /**
     * 统一响应结果封装
     */
    private Map<String, Object> buildResponse(int code, String message, Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        response.put("data", data);
        return response;
    }
    /**
     * 查询所有启用的轮播图（用于前端展示）
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> selectActiveCarousels() {
        try {
            List<Carouse> carouses = carousService.selectActiveCarousels();
            return ResponseEntity.ok(buildResponse(200, "查询成功", carouses));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(buildResponse(500, "查询失败：" + e.getMessage(), null));
        }
    }
    /**
     * 根据ID查询单个轮播图（用于编辑功能）
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> selectById(@PathVariable Long id) {
        try {
            Carouse carouse = carousService.selectById(id);
            if (carouse == null) {
                return ResponseEntity.ok(buildResponse(404, "轮播图不存在", null));
            }
            return ResponseEntity.ok(buildResponse(200, "查询成功", carouse));
        } catch (IllegalArgumentException e) {
            // 捕获参数校验异常（如ID不合法）
            return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(buildResponse(500, "查询失败：" + e.getMessage(), null));
        }
    }
    /**
     * 新增轮播图
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> insert(@RequestBody Carouse carouse) {
        try {
            int rows = carousService.insert(carouse);
            return ResponseEntity.ok(buildResponse(200, rows > 0 ? "新增成功" : "新增失败", rows));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(buildResponse(500, "新增失败：" + e.getMessage(), null));
        }
    }
    /**
     * 更新轮播图
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> update(@RequestBody Carouse carouse) {
        try {
            int rows = carousService.update(carouse);
            return ResponseEntity.ok(buildResponse(200, rows > 0 ? "更新成功" : "更新失败", rows));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(buildResponse(500, "更新失败：" + e.getMessage(), null));
        }
    }
    /**
     * 根据ID删除轮播图
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteById(@PathVariable Long id) {
        try {
            int rows = carousService.deleteById(id);
            return ResponseEntity.ok(buildResponse(200, rows > 0 ? "删除成功" : "删除失败", rows));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(buildResponse(500, "删除失败：" + e.getMessage(), null));
        }
    }
}
